package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.HashMapUtils;
import com.j283_5.wisdom_park.common.utils.MD5Utils;
import com.j283_5.wisdom_park.mapper.*;
import com.j283_5.wisdom_park.pojo.*;
import com.j283_5.wisdom_park.service.OpelogmanageService;
import com.j283_5.wisdom_park.service.Peree_PersonService;
import com.j283_5.wisdom_park.vo.contract_management.CustomerVo;
import com.j283_5.wisdom_park.vo.tenant_management.PersonVo;
import com.j283_5.wisdom_park.vo.tenant_management.Person_PereeVo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class Peree_PersonServiceImpl implements Peree_PersonService {
    @Resource
    private PersonMapper mapper;
    @Resource
    private PereeMapper pereeMapper;
    @Resource
    private LoginMapper loginMapper;
    @Override
    public Map<String, Object> findPerson(PersonVo personVo, Integer page, Integer limit) {
        Integer start=null;
        //总条数
        Long countRows=null;
        List<PersonVo> list=null;
        if (page!=null&&limit!=null){
            start=(page-1)*limit;
        }
        countRows=mapper.findPersonCount(personVo);
        //如果总条数大于0  才去查询数据
        if(countRows!=null && countRows>0){
            list=mapper.findPerson(personVo,start,limit);
            return new HashMapUtils()
                    .addData("countRows",countRows)
                    .addData("listCustomer",list)
                    .getMap();
        }else{
            return new HashMapUtils().getMap();
        }
    }

    /**
     * 查询人员详情
     * @param personId
     * @return
     */
    @Override
    public List<Person> findPersonInfo(Integer personId) {
        List<Person> personInfo = mapper.findPersonInfo(personId);
        return personInfo;
    }

    /**
     * 查询人员所属信息
     * @param personId
     * @return
     */
    @Override
    public List<Person_PereeVo> findPerson_PereeVo(Integer personId) {
        List<Person_PereeVo> person_pereeVo = mapper.findPerson_PereeVo(personId);
        return person_pereeVo;
    }

    /**
     * 删除人员
     * @param listPersonId
     * @return
     */
    @Override
    public ResultData deletePerson(List<Integer> listPersonId) {
        UpdateWrapper<Person> PersonUpdateWrapper = new UpdateWrapper<>();
        PersonUpdateWrapper.set("person_delete",1);
        PersonUpdateWrapper.in("person_id",listPersonId);
        int i = mapper.update(null, PersonUpdateWrapper);
        if(i > 0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.error().message("删除失败！");

    }

    /**
     * 新增租户人员
     * @param
     * @param person_pereeVo
     * @return
     */
    @Override
    public ResultData insertPerson(Person_PereeVo person_pereeVo) {
            Person person=new Person();
            person.setPersonName(person_pereeVo.getPersonName());
            person.setPerId(person_pereeVo.getPerId());
            person.setPersonPhone(person_pereeVo.getPersonPhone());
            person.setPersonSex(person_pereeVo.getPersonSex());
            person.setPersonEaducation(person_pereeVo.getPersonEaducation());
            person.setPersonVx(person_pereeVo.getPersonVx());
            person.setPersonPhoto(person_pereeVo.getPersonPhoto());
            person.setPersonType(person_pereeVo.getPersonType());

        Login login=new Login();
        login.setLogPassword(MD5Utils.md5(person_pereeVo.getLogPassword()));

        int insert1 = loginMapper.insert(login);
        Integer logId = login.getLogId();

        person.setLog_id(logId);


        int insert = mapper.insert(person);
        if (insert>0){
            return ResultData.ok().message("插入成功");
        }else {
            return ResultData.ok().message("插入失败");
        }
    }

    /**
     * 进入新增界面
     * @return
     */
    @Override
    public ResultData PersonInsertInterface() {
        QueryWrapper<Peree> pereeQueryWrapper = new QueryWrapper<>();
        pereeQueryWrapper.select("per_id,per_name");
        List<Peree> perees = pereeMapper.selectList(pereeQueryWrapper);
        return ResultData.ok().data(perees);
    }
}
